package com.yg.controller;


import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yg.config.result.R;
import com.yg.entity.singleEntity.Dictionary;
import com.yg.entity.singleEntity.QualityTeam;
import com.yg.entity.singleEntity.QualityTitle;
import com.yg.service.DictionaryService;
import com.yg.service.QualityTitleService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * sheet06
 */
@RestController
@RequestMapping("/qualityTitle")
@CrossOrigin
@Api(tags = "对应第六张表")
public class QualityTitleController {

    @Autowired
    private QualityTitleService qualityTitleService;

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 添加质量工程头衔
     *
     * @param qualityTitle
     * @return
     */
    @ApiOperation("添加质量工程头衔")
    @PostMapping("postQualityTitle")
    public R postTeacher(
            @ApiParam(name = "质量工程头衔数据") @RequestBody(required = true) QualityTitle qualityTitle
    ) {
        boolean flag = qualityTitleService.save(qualityTitle);

        return flag ? R.OK() : R.ERROR().message("添加失败");
    }

    /**
     * 获得所有质量工程头衔信息
     *
     * @param current 当前页
     * @param limit   每页限制的条目
     * @return
     */
    @ApiOperation("获得所有质量工程头衔信息")
    @GetMapping("getAllQualityTitle")
    public R getAllTeacher(
            @ApiParam(name = "current", value = "当前页", required = true) @RequestParam(name = "current") long current,
            @ApiParam(name = "limit", value = "每页显示的条目数", required = true) @RequestParam(name = "limit") long limit
    ) {
        try {
            Page<QualityTitle> page = new Page<>(current, limit);
            QueryWrapper<QualityTitle> wrapper = new QueryWrapper<>();
            wrapper.eq("deleted", 0);
            qualityTitleService.page(page, wrapper);
            List<QualityTitle> list = page.getRecords();
            long total = page.getTotal();
            long pages = page.getPages();

            List<Dictionary> title = dictionaryService.getTitle();


            return R.OK().data("title", title).data("qualityTitleList", list).data("total", total).data("pages", pages);
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().message("查询失败");
        }
    }

    /**
     * 条件查询质量工程头衔数据
     *
     * @param current      当前页
     * @param limit        每页限制的条目
     * @param qualityTitle 查询条件
     * @return
     */
    @ApiOperation("条件查询质量工程头衔数据")
    @PostMapping("selectQualityTitleByCondition")
    public R selectTeacherByCondition(
            @ApiParam(name = "current", value = "当前页", required = false) @RequestParam(name = "current") long current,
            @ApiParam(name = "limit", value = "每页显示的条目数", required = false) @RequestParam(name = "limit") long limit,
            @ApiParam(name = "qualityTitle", value = "质量工程头衔表", required = false) @RequestBody(required = false) QualityTitle qualityTitle
    ) {
        try {
            Page<QualityTitle> page = new Page<>(current, limit);

            QueryWrapper<QualityTitle> wrapper = new QueryWrapper<>();
//            wrapper.eq("deleted", 0);
            if (!StringUtils.isEmpty(qualityTitle.getName())) {
                wrapper.like("name", qualityTitle.getName());
            }
            if (!StringUtils.isEmpty(qualityTitle.getTitle())) {
                wrapper.eq("title", qualityTitle.getTitle());
            }

            qualityTitleService.page(page, wrapper);
            List<QualityTitle> list = page.getRecords();
            long total = page.getTotal();
            long pages = page.getPages();

            return R.OK().data("qualityTitleList", list).data("total", total).data("pages", pages);
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().message("查询失败");
        }

    }

    /**
     * 质量工程头衔修改
     *
     * @param qualityTitle
     * @return
     */
    @ApiOperation("质量工程头衔修改")
    @PutMapping("putQualityTitle")
    public R putTeacher(
            @ApiParam(name = "qualityTitle", value = "修改后的头衔数据", required = false) @RequestBody QualityTitle qualityTitle
    ) {
        QueryWrapper<QualityTitle> wrapper = new QueryWrapper<>();
        wrapper.eq("id", qualityTitle.getId());
        boolean flag = qualityTitleService.update(qualityTitle, wrapper);
        return flag ? R.OK() : R.ERROR().message("修改失败");
    }

    /**
     * 删除质量工程头衔信息
     *
     * @param qualityTitle
     * @return
     */
    @ApiOperation("删除质量工程头衔信息")
    @DeleteMapping("deleteQualityTitle")
    public R deleteTeacher(@ApiParam(name = "qualityTitle", value = "头衔数据", required = false) @RequestBody QualityTitle qualityTitle) {

        String id = null;
        try {
            id = qualityTitle.getId();
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().message("查询信息错误");
        }

        try {
            boolean flag = qualityTitleService.removeById(id);
            return flag ? R.OK() : R.ERROR();
        } catch (Exception e) {
            e.printStackTrace();
            return R.ERROR().message("删除失败");
        }
    }

}
